package com.excesys.exsecs.scada.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.scada.entity.ScadaFolder;
import com.excesys.exsecs.scada.entity.ScadaFolderAndLinked;
import com.excesys.exsecs.scada.entity.ScadaProperty;
import com.excesys.exsecs.scada.service.ScadaFolderAndLinkedService;
import com.excesys.exsecs.scada.service.ScadaFolderService;
import com.excesys.exsecs.scada.service.ScadaPropertyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件夹_
 *
 * @author pig
 * @date 2024-06-25 09:18:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/scadaAndLinked" )
@Tag(description = "scadaAndLinked" , name = "组态绑定规则" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ScadaFolderAndLinkedController {

    private final ScadaFolderAndLinkedService service;


	/**
     * 分页查询
     * @param page 分页对象
     * @param scadaFolderAndLinked 文件夹_
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
   // @PreAuthorize("@pms.hasPermission('scadaAndLinked_view')" )
    public R getScadaFolderPage(@ParameterObject Page page, @ParameterObject ScadaFolderAndLinked scadaFolderAndLinked) {
        LambdaQueryWrapper<ScadaFolderAndLinked> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ScadaFolderAndLinked::getParentId,scadaFolderAndLinked.getParentId());
        return R.ok(service.page(page, wrapper));
    }

	/**
	 *  场景绑定规则
	 */
	@PostMapping
	public  R bindLinkedId(@RequestBody List<ScadaFolderAndLinked> scadaFolderAndLinkeds){
		//绑定规则引擎必定存在父级
		service.deleteFromParentId(scadaFolderAndLinkeds.get(0).getParentId());
		for(ScadaFolderAndLinked scadaFolderAndLinked : scadaFolderAndLinkeds){
			service.save(scadaFolderAndLinked);
		}
		return R.ok();
	}
	@DeleteMapping("/{id}")
   public R deleteById(@PathVariable Integer id){
		return R.ok(service.removeById(id));
	}

}